/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mascotikas.client.animals;

/**
 *
 * @author aneira
 */
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.events.ItemChangedEvent;
import com.smartgwt.client.widgets.form.events.ItemChangedHandler;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.tile.TileGrid;
import com.smartgwt.client.widgets.tile.events.RecordClickEvent;
import com.smartgwt.client.widgets.tile.events.RecordClickHandler;
import com.smartgwt.client.widgets.viewer.DetailFormatter;
import com.smartgwt.client.widgets.viewer.DetailViewer;
import com.smartgwt.client.widgets.viewer.DetailViewerField;

import java.util.LinkedHashMap;

public class AnimalsTileGrid extends TileGrid {

    // private VLayout pnlAnimales;
    private DynamicForm sortForm = new DynamicForm();
    private DetailViewerField ubicacionField = new DetailViewerField("locate");
    private DetailViewerField pictureField = new DetailViewerField("picture");
    private DetailViewerField imgDetalleField = new DetailViewerField("picture");
    private DetailViewerField commonNameField = new DetailViewerField("name");
    private DetailViewerField typePublishField = new DetailViewerField("typePublish");
    private DetailViewerField commnetField = new DetailViewerField("comment", "Descripcion");

    public AnimalsTileGrid(String categoria) {

        commonNameField.setCellStyle("commonName");
        ubicacionField.setCellStyle("ubicacion");
        commonNameField.setDetailFormatter(new DetailFormatter() {

            public String format(Object value, Record record, DetailViewerField field) {
                return "Nombre : " + value;
            }
        });
        setFields(pictureField, ubicacionField, commonNameField, typePublishField);
        SelectItem sortItem = new SelectItem();
        sortItem.setName("ordenado");
        sortItem.setTitle("Ordenar por");

        LinkedHashMap valueMap = new LinkedHashMap();
        valueMap.put("locate", "Ubicacion");
        valueMap.put("name", "Nombre");
        valueMap.put("typePublish", "Tipo Publicacion");
        sortItem.setValueMap(valueMap);
        final CheckboxItem ascendingItem = new CheckboxItem("chkSortDir");
        ascendingItem.setTitle("Ascendente");

        sortForm.setFields(sortItem, ascendingItem);

        sortForm.addItemChangedHandler(new ItemChangedHandler() {

            public void onItemChanged(ItemChangedEvent event) {
                String sortVal = sortForm.getValueAsString("ordenado");
                Boolean sortDir = (Boolean) ascendingItem.getValue();
                if (sortDir == null) {
                    sortDir = false;
                }
                if (sortVal != null) {
                    sortByProperty(sortVal, sortDir);
                }
            }
        });
        sortForm.setIsGroup(true);
        sortForm.setGroupTitle("Ordenado");
        sortForm.setAutoFocus(false);
        sortForm.setNumCols(6);
        setTileWidth(205);
        setTileHeight(120);
        setAutoFetchData(true);
        setAnimateTileChange(true);
        buildDetailView(categoria);
    }

    private void buildDetailView(String categoria) {

        final DetailViewer detailViewer = new DetailViewer();
        detailViewer.setWidth(500);
        detailViewer.setTop(400);
        detailViewer.setEmptyMessage("Click a row in the grid");

        imgDetalleField.setType("image");
        imgDetalleField.setImageWidth(400);
        imgDetalleField.setImageHeight(300);
        addRecordClickHandler(new RecordClickHandler() {

            public void onRecordClick(RecordClickEvent event) {
                detailViewer.setFields(imgDetalleField, new DetailViewerField("name", "Nombre"), commnetField);
                final Window winModal = new Window();
                winModal.setAutoSize(Boolean.TRUE);
                winModal.setTitle("Detalle");
                winModal.setShowMinimizeButton(false);
                winModal.setIsModal(true);
                winModal.setShowModalMask(true);
                winModal.setAutoCenter(Boolean.TRUE);
                winModal.addCloseClickHandler(new CloseClickHandler() {

                    public void onCloseClick(CloseClientEvent event) {
                        setTitle("cerrar");
                        winModal.destroy();
                    }
                });
                detailViewer.setData(getSelection());
                winModal.addItem(detailViewer);
                winModal.show();
            }
        });
    }

    public DynamicForm getSortAnimals() {
        return sortForm;

    }
}
